<template>
  <div class="user-center">
    <el-card class="main-card">
      <template #header>
        <div class="card-header">
          <span class="title">个人中心</span>
        </div>
      </template>
      
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="user-form"
      >
        <el-form-item label="头像">
          <el-avatar :size="100" :src="IMAGE_URL + form.avatar" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" disabled />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" disabled />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit" :loading="loading">保存修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/modules/user'
import { getUserInfoService, updateUserInfoService } from '@/api/user'
import { IMAGE_URL } from '@/utils/request'

const userStore = useUserStore()
const formRef = ref(null)
const loading = ref(false)

const form = ref({
  userId: '',
  username: '',
  phone: '',
  nickName: '',
  sex: '',
  avatar: ''
})

const rules = {
  nickName: [
    { required: true, message: '请输入昵称', trigger: 'blur' },
    { min: 2, max: 20, message: '昵称长度在2到20个字符之间', trigger: 'blur' }
  ],
  sex: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ]
}

// 获取用户信息
const getUserInfo = async () => {
  try {
    const res = await getUserInfoService()
    Object.assign(form.value, res.data)
  } catch (error) {
    console.error('获取用户信息失败:', error)
    ElMessage.error('获取用户信息失败')
  }
}

// 提交修改
const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const { nickName, sex } = form.value
        await updateUserInfoService({ nickName, sex })
        ElMessage.success('修改成功')
        // 更新用户信息
        await userStore.getUserInfo()
      } catch (error) {
        console.error('修改失败:', error)
        ElMessage.error('修改失败')
      } finally {
        loading.value = false
      }
    }
  })
}

onMounted(() => {
  getUserInfo()
})
</script>

<style scoped>
.user-center {
  padding: 20px;
}

.main-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
}

.user-form {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
}

:deep(.el-input__inner) {
  height: 40px;
}

:deep(.el-button) {
  width: 100%;
  height: 40px;
  margin-top: 10px;
}

:deep(.el-avatar) {
  border: 2px solid #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style> 